package config;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseConfig {

    // 固定的数据库连接信息
    private static final String URL = "jdbc:mysql://localhost:3306/people";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "nmnm1239";

    static {
        try {
            // 加载 MySQL JDBC 驱动程序
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new ExceptionInInitializerError("MySQL JDBC Driver not found.");
        }
    }

    public static Connection getConnection() throws SQLException {
        Connection connection = null;

        try {
            // 建立数据库连接
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new SQLException("Error establishing database connection.");
        }

        return connection;
    }
}
